# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Kasei Wang <cnsdwpc at gmail.com>

pkgname=kcptun
pkgver=20231012
_commit=0440abbc9124db0b67143cd12cc71a3ac2584bea
pkgrel=1
pkgdesc="A Secure Tunnel Based On KCP with N:M Multiplexing"
arch=('loong64' 'x86_64')
url="https://github.com/xtaci/kcptun"
license=('MIT')
depends=('glibc')
makedepends=('go' 'git')
options=('!lto')
source=("git+https://github.com/xtaci/kcptun.git#commit=$_commit"
        'kcptun@.service' 'kcptun-server@.service')
sha512sums=('SKIP'
            'f6041eae26db4f11bf6dc22443548354114b8b5a2a436949513a168d5b271707e680dd1d6bdcfc4385945ae2cb1bc82736e605ca49c6c94904922b43d1917a5c'
            '88607e7c6cba7628bf2c9229396fcf285227c56d2fd208f3d0200fb670f8919939ef05aa5e87fc3aceedb4f41e5bca597138c88930a23dbe4b03e22b42d64cec')

prepare() {
  cd kcptun

  mkdir -p .gopath/src/github.com/xtaci
  ln -sf "$PWD" .gopath/src/github.com/xtaci/kcptun
  export GOPATH="$PWD/.gopath:/usr/share/gocode"
}

build() {
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-buildmode=pie -trimpath -mod=readonly -modcacherw"

  cd "$srcdir"/kcptun/client
  go build -o build/kcptun-client -ldflags "-X main.VERSION=$pkgver -extldflags=${LDFLAGS}" # -gccgoflags "$CFLAGS $LDFLAGS"

  cd "$srcdir"/kcptun/server
  go build -o build/kcptun-server -ldflags "-X main.VERSION=$pkgver -extldflags=${LDFLAGS}" # -gccgoflags "$CFLAGS $LDFLAGS"
}

package() {
  cd kcptun

  install -Dm755 client/build/kcptun-client -t "$pkgdir"/usr/bin/
  install -Dm755 server/build/kcptun-server -t "$pkgdir"/usr/bin/

  install -Dm644 LICENSE.md -t "$pkgdir"/usr/share/licenses/$pkgname/

  install -dm755 "$pkgdir"/etc/kcptun
  install -Dm644 "$srcdir"/kcptun@.service -t "$pkgdir"/usr/lib/systemd/system/
  install -Dm644 "$srcdir"/kcptun-server@.service -t "$pkgdir"/usr/lib/systemd/system/
}
